package com.zxtyshangjia.zxtyjms;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.LocalBroadcastManager;
import android.view.KeyEvent;
import android.view.View;
import android.widget.RadioButton;
import android.widget.Toast;

import com.zxtyshangjia.zxtyjms.business.fragment.ThreeFragment;
import com.zxtyshangjia.zxtyjms.first.fragment.OneFragment;
import com.zxtyshangjia.zxtyjms.order.fragment.TwoFragment;
import com.zxtyshangjia.zxtyjms.utils.LogUtil;


public class MainActivity extends FragmentActivity implements View.OnClickListener{
    private RadioButton btnHome, btnMessage, btnCategory;
    private FragmentManager mFragmentManager;
    private Fragment mOneFragment, mTwoFragment, mThreeFragment;

    //   private Vibrator vibrator;
    /** 本地广播管理器 */
    private LocalBroadcastManager mLocalBroadManager = null;
    private MyBroadcastReceover mBroadcastReceiver = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         /* 注册本地广播接收器 */
        mLocalBroadManager = LocalBroadcastManager.getInstance(this);
        IntentFilter filter = new IntentFilter(this.getClass().getName());
        mBroadcastReceiver = new MyBroadcastReceover();
        mLocalBroadManager.registerReceiver(mBroadcastReceiver,filter);
        bindViews();
        bindListener();
        initData();
    }

    private void initData() {
        mFragmentManager = getSupportFragmentManager();

        hideAllFragments();
        if (null == mOneFragment) {
            mOneFragment = Fragment.instantiate(this, OneFragment.class.getName());
            addFragment(mOneFragment);
        } else {
            showFragment(mOneFragment);
        }
    }

    private void bindViews() {
        btnHome = (RadioButton) findViewById(R.id.one);
        btnMessage = (RadioButton) findViewById(R.id.two);
        btnCategory = (RadioButton) findViewById(R.id.three);
    }

    private void bindListener() {
        btnHome.setOnClickListener(this);
        btnCategory.setOnClickListener(this);
        btnMessage.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.one:
                hideAllFragments();
                showFragment(mOneFragment);
                break;
            case R.id.two:
                hideAllFragments();
                if (null == mTwoFragment) {
                    mTwoFragment = Fragment.instantiate(this, TwoFragment.class.getName());
                    addFragment(mTwoFragment);
                } else {
                    showFragment(mTwoFragment);
                }
                break;
            case R.id.three:
                hideAllFragments();
                if (null == mThreeFragment) {
                    mThreeFragment = Fragment.instantiate(this, ThreeFragment.class.getName());
                    addFragment(mThreeFragment);
                } else {
                    showFragment(mThreeFragment);
                }
                break;

        }
    }

    private void hideFragment(Fragment fragment) {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        transaction.hide(fragment);
        transaction.commit();
    }

    private void showFragment(Fragment fragment) {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        transaction.show(fragment);
        transaction.commit();
    }

    public void addFragment(Fragment fragment) {
        FragmentTransaction transaction = mFragmentManager.beginTransaction();
        transaction.add(R.id.fragment_content, fragment);
        transaction.commit();
    }

    private void hideAllFragments() {
        if (null != mOneFragment) {
            hideFragment(mOneFragment);
        }

        if (null != mTwoFragment) {
            hideFragment(mTwoFragment);
        }

        if (null != mThreeFragment) {
            hideFragment(mThreeFragment);
        }

    }

    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);

        if (mOneFragment == null && fragment instanceof OneFragment) {
            mOneFragment = fragment;
        } else if (mTwoFragment == null && fragment instanceof TwoFragment) {
            mTwoFragment = fragment;
        } else if (mThreeFragment == null && fragment instanceof ThreeFragment) {
            mThreeFragment = fragment;
        }
    }

    //点击两次退出程序
    private long exitTime = 0;

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if ((System.currentTimeMillis() - exitTime) > 2000) {
                Toast.makeText(getApplicationContext(), "再按一次退出程序",
                        Toast.LENGTH_SHORT).show();
                exitTime = System.currentTimeMillis();
            } else {
                finish();
//                System.exit(0);
                System.gc();
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    public class MyBroadcastReceover extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            finish();
            LogUtil.i(MainActivity.class.getName(), "==> : finish() success");
            //启动系统垃圾回收机制
            System.gc();
        }
    }

}
